import { getTypeOf } from "@/utils";
export interface ResultType {
  content: Nullable<any>;
  loading: boolean;
  errCode: number;
  errText: string;
}

// 词典结果数据转换
export function adaptDictData(data: any) {
  const dictResult = {
    content: null,
    loading: false,
    errCode: 0,
    errText: "",
  } as ResultType;

  if (data?.dict) {
    try {
      data.dict = JSON.parse(data.dict);
    } catch (e) {
      data.dict = null;
    }
    if (getTypeOf(data.dict) !== "object") {
      data.dict = null;
    }
  }

  if (data?.dict?.word_result?.simple_means) {
    const simple_means = {} as any;
    if (data.dict.lang == 1) {
      simple_means.symbols = data.dict.word_result.simple_means.symbols.map(
        (symbol: any) => {
          return {
            ph_en: symbol.ph_en,
            ph_am: symbol.ph_am,
            parts: symbol.parts.map((part: any) => ({
              part: part.part,
              means: part.means,
            })),
          };
        }
      );
      simple_means.exchange = data.dict.word_result.simple_means.exchange;
    } else {
      simple_means.symbols = data.dict.word_result.simple_means.symbols.map(
        (symbol: any) => {
          return {
            word_symbol: symbol.word_symbol,
            parts: symbol.parts.map((part: any) => ({
              part_name: part.part_name,
              means_text: part.means
                .map((it: any) => {
                  if (typeof it === "string") {
                    return it;
                  }
                  return it.word_mean;
                })
                .filter((it: any) => !!it)
                .join("；"),
            })),
          };
        }
      );
    }
    const zdict = data.dict.word_result.zdict;
    const edict = data.dict.word_result.edict;
    data.dict.word_result = {
      simple_means, // 过滤不必要的数据
      zdict,
      edict,
    };
    dictResult.content = data;
  } else {
    dictResult.errCode = 404;
    dictResult.errText = "很抱歉请求不到该单词的释义，请检查单词拼写是否准确。";
  }

  return dictResult;
}

/**
 * 获取默认词典数据，用于词典错误时遮罩层下显示
 * @returns {Objct}
 */
export function getDefaultDict(){
  return {
    "dict": "{\"lang\":\"0\",\"word_result\":{\"common\":{\"from\":\"optimize\",\"text\":\"hello\"},\"edict\":\"\",\"simple_means\":{\"from\":\"CEDict\",\"symbols\":[{\"parts\":[{\"means\":[{\"means\":[\"你好\",\"(用于问候、接电话或引起注意)喂\",\"(表示惊讶或认为别人说了蠢话或没有注意听)嘿\"],\"part\":\"int.\\/n.\",\"text\":\"hello\",\"word_mean\":\"hello\"},{\"means\":[\"（用于非正式场合）嗨，你好\",\"（用于打招呼）喂，嗨\"],\"part\":\"int.\",\"text\":\"hi\",\"word_mean\":\"hi\"},{\"means\":[\"你好吗\",\"你过得好吗\"],\"part\":\"\",\"text\":\"how are you\",\"word_mean\":\"how are you\"},{\"means\":[\"您好\"],\"part\":\"\",\"text\":\"how do you do\",\"word_mean\":\"how do you do\"},{\"means\":[\"(招呼语)你好\"],\"part\":\"int.\",\"text\":\"howdy\",\"word_mean\":\"howdy\"},{\"means\":[\"下午好，（下午见面时用语）你（们）好（非正式场合常说Afternoon）\"],\"part\":\"int.\",\"text\":\"good afternoon\",\"word_mean\":\"good afternoon\"},{\"means\":[\"（晚上见面时用语）晚上好，你（们）好（非正式场合常说 Evening）\"],\"part\":\"int.\",\"text\":\"good evening\",\"word_mean\":\"good evening\"},{\"means\":[\"你好\",\"喂\"],\"part\":\"int.\",\"text\":\"g'day\",\"word_mean\":\"g'day\"},{\"means\":[\"你好\",\"嗨\"],\"part\":\"int.\",\"text\":\"wotcha\",\"word_mean\":\"wotcha\"},{\"means\":[\"你好\",\"祝你健康\"],\"part\":\"int.\",\"text\":\"kia ora\",\"word_mean\":\"kia ora\"},{\"means\":[\"(打招呼用语)你好\"],\"part\":\"int.\",\"text\":\"howzit\",\"word_mean\":\"howzit\"},{\"means\":[\"你好\",\"是\",\"对\",\"喂\"],\"part\":\"int.\",\"text\":\"yebo\",\"word_mean\":\"yebo\"}],\"part_name\":\"\"}],\"word_symbol\":\"nǐ hǎo\"}],\"word_means\":[\"hello\",\"hi\",\"how are you\",\"how do you do\",\"howdy\",\"good afternoon\",\"good evening\",\"g'day\",\"wotcha\",\"kia ora\",\"howzit\",\"yebo\"],\"word_name\":\"你好\"},\"synthesize_means\":{\"symbols\":[{\"cys\":[{\"means\":[{\"cy_id\":\"11478\",\"mean_id\":\"12614\",\"part_id\":null,\"word_mean\":\"how do you do; how are you; hello\"}]}],\"parts\":[],\"symbol_id\":\"11714\",\"word_id\":\"11638\",\"word_symbol\":\"nǐ hǎo\",\"xg\":\"\"}],\"word_id\":\"11638\",\"word_name\":\"你好\"},\"zdict\":{\"detail\":null,\"simple\":{\"chenyu\":null,\"means\":[{\"exp\":[{\"des\":[{\"main\":\"[how do you do;how are you;hello] 用于有礼貌的打招呼或表示与人见面时的问候\",\"sub\":[]}],\"pos\":\"\"}],\"pinyin\":\"nǐ hǎo\"}]},\"word\":\"你好\"}}}"
  }
}